#include "Utils.h"

#include <sstream>
#include <stdlib.h>
#include <time.h> 


bool Utils::m_SeedInitialized = false;

Utils::Utils()
{
}

Utils::~Utils()
{
	
}

std::string Utils::convertIntToStr(int number){
	std::string result;
	std::ostringstream convert;   // stream used for the conversion
	
	convert << number;      // insert the textual representation of 'Number' in the characters in the stream
	result = convert.str(); // set 'Result' to the contents of the stream
	
	return result;	
}

int Utils::convertSrtToInt(std::string str){
	int result;
	
	std::istringstream convert(str); // stringstream used for the conversion constructed with the contents of 'Text' 
	// ie: the stream will start containing the characters of 'Text'
	convert >> result;
	
	return result;
}

void Utils::initializeSeed()
{
	/* initialize random seed: */
	srand (time(NULL));
	m_SeedInitialized = true;
}

int Utils::generateRandNumber(int maxValor){
	if(!m_SeedInitialized)
	{
		Utils::initializeSeed();
	}
	
	/* generate secret number between 1 and cantMaxDigitos * 10: */
	return rand() % maxValor + 1;
}

std::string Utils::getNanoTime()
{
	timespec ts;
	clock_gettime(CLOCK_REALTIME, &ts);
	std::string nanoTime = Utils::convertIntToStr(ts.tv_sec)+Utils::convertIntToStr(ts.tv_nsec);
	return nanoTime;
}